<?php

namespace App\Common\Comp;

use App\Common\Oss\OssObject;
use Illuminate\Support\Facades\Log;

/**
 * Class UploadComp
 * @package App\Common\Comp
 */
class UploadComp
{

    public function __construct()
    {
        //初始化返回值
        $this->resDate = ['code' => 200, 'msg' => '', 'data' => []];
    }

    /**
     * 文件上传接口
     * @param $type
     * @param $body
     * @param $fileName
     * @return array|string
     */
    public function upload($type, $body, $fileName)
    {
        $config = config('BaseConfig');
        $bucket = $config['oss']['bucket'];
        $scene = '';
        switch ($type) {
            // 1-用户
            case 1:
                $path = $config['oss']['user'] . '/' . date('Ym') . '/' . $fileName;
                $scene = 'user';
                break;
            // 2-短视频
            case 2:
                $path = $config['oss']['shortVideo'] . '/' . date('Ym') . '/' . $fileName;
                $scene = 'shortVideo';
                break;
            // 3-服务
            case 3:
                $path = $config['oss']['course'] . '/' . date('Ym') . '/' . $fileName;
                $scene = 'course';
                break;
            // 4-直播
            case 4:
                $path = $config['oss']['live'] . '/' . date('Ym') . '/' . $fileName;
                $scene = 'live';
                break;
            // 5-其他
            case 5:
                $path = $config['oss']['other'] . '/' . date('Ym') . '/' . $fileName;
                $scene = 'other';
                break;
            // 6-礼物（礼物图和守护图）
            case 6:
                $path = $config['oss']['gift'] . '/' . $fileName;
                $scene = 'gift';
                break;
            // 7-系统（大转盘，服务标签，）
            case 7:
                $path = $config['oss']['system'] . '/' . $fileName;
                $scene = 'system';
                break;

            // 8-cos
            case 8:
                $path = $config['oss']['cos'] . '/' . $fileName;
                $scene = 'cos';
                break;

            // 9-合同
            case 9:
                $path = $config['oss']['contract'] . '/' . $fileName;
                $scene = 'contract';
                break;

            // 10-视频榜单轮播图&搜索榜单轮播图
            case 10:
                $path = $config['oss']['carousel'] . '/' . $fileName;
                $scene = 'contract';
                break;

            // 11-apk ios 包上传
            case 11:
                $path = $config['oss']['package'] . '/' . $fileName;
                $scene = 'package';
                break;
            // 动效
            case 12:
                $path = $config['oss']['effect'] . '/' . $fileName;
                $scene = 'effect';
                break;
            // excel
            case 13:
                $path = $config['oss']['excel'] . '/' . $fileName;
                $scene = 'excel';
                break;
            // 14-系统（轮播图使用）
            case 14:
                $path = $config['oss']['system'] . '/' . $fileName;
                $scene = 'system';
                break;

        }
        $oss = new OssObject($bucket);
        $res = $oss->putObject($body, $path, []);

        if (empty($res)) {
            Log::info('OSS上传失败' . $res);
            return [];
        }
        $res['bucket'] = $bucket;
        $res['key'] = $fileName;
        $res['path'] = $path;
        $res['scene'] = $scene;
        return $res;
    }
}
